YAHOO.namespace('springmole');

(function () {
	
	/**
	 * 
	 * @param {Object} el
	 * @param {Object} userConfig
	 */
	YAHOO.springmole.StatusPanel = function (el, userConfig) {
		YAHOO.springmole.StatusPanel.superclass.constructor.call(this, el, userConfig);
	};
	
	var Dom = YAHOO.util.Dom,
		Element = YAHOO.util.Element,
		Event = YAHOO.util.Event,
		Connect = YAHOO.util.Connect,
		
		HttpManager = YAHOO.springmole.HttpManager,
		
		REQUEST_TYPE_CLASS = 'request-class',
		SUCCESS_TYPE_CLASS = 'success-class',
		ERROR_TYPE_CLASS = 'error-class';
		
	
	function appendConnectionStatus(type, args) {
		this.appendStatus(args[0].tId + " " + type, type);
	}
	
	YAHOO.lang.extend(YAHOO.springmole.StatusPanel, YAHOO.springmole.SpringMolePanel, {
		
		/**
		 * <ol> YUI Element on witch every message <li> Element will be appended
		 */
		_olElement: null,
		
		_oScroll: null,
		
		
		init : function (el, userConfig) {
			YAHOO.springmole.StatusPanel.superclass.init.call(this, el, userConfig);
			
			Connect.startEvent.subscribe(appendConnectionStatus, this, true);
			Connect.completeEvent.subscribe(appendConnectionStatus, this, true);
			Connect.successEvent.subscribe(appendConnectionStatus, this, true);
			Connect.failureEvent.subscribe(appendConnectionStatus, this, true);
			Connect.abortEvent.subscribe(appendConnectionStatus, this, true);		
		},
		  
		initDefaultConfig: function () {
			YAHOO.springmole.StatusPanel.superclass.initDefaultConfig.call(this);
		},
		
		render: function (parentNode) {
			YAHOO.springmole.StatusPanel.superclass.render.call(this, parentNode);
			
			var olId = Dom.generateId();
			
			this.setHeader("Status");
			this.setBody('<ol id="' + olId + '"></ol>');
			
			Event.onAvailable(olId, function () {
				this._olElement = new Element(olId);
				
				this._oScroll = new YAHOO.util.Scroll(this.body, { 
					scroll: { to: [0, 1000] } 
				});
			}, this, true);
						
			this._panelResize.on('resize', function (args) {
				Dom.setStyle(this.body, 'height', (args.height - 46) + 'px');
			}, this, true);
			
			// Set initial body size
			Dom.setStyle(this.body, 'height', '14px');
		},
		
		appendStatus: function (message, typeClass) {
			var liElement = new Element(document.createElement('li'));
			liElement.get('element').innerHTML = message;
			liElement.addClass(typeClass);
			liElement.appendTo(this._olElement);
			
			this._oScroll.animate();
		},
		
		appendRequest: function (message) {
			this.appendStatus(message, REQUEST_TYPE_CLASS);
		},
		
		appendSuccess: function (message) {
			this.appendStatus(message, SUCCESS_TYPE_CLASS);
		},
		
		appendError: function (message) {
			this.appendStatus(message, ERROR_TYPE_CLASS);
		}
		
		
	});

})();